package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.anno.PermissionName;
import cn.wolfcode.education.domain.Dept;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.service.IDeptService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;


    @RequestMapping("")
    @PermissionName("主页面管理")
    @RequiresPermissions("dept:view")
   public String index(){
        return "dept";
    }


    @PermissionName("部门页面")
    @RequestMapping("list")
    @RequiresPermissions(("dept:list"))
    @ResponseBody
    public PageResult list(QueryObject qo) {
       return   deptService.list(qo);

    }

    @PermissionName("部门保存")
    @RequestMapping("save")
    @RequiresPermissions("dept:save")
    public JSONResult save(Dept dept){
      JSONResult jsonResult = new JSONResult();
      try {
          deptService.save(dept);
      }catch (Exception e){
          e.printStackTrace();
          jsonResult.setMsg("保存失败");
      }
      return jsonResult;
    }



    @PermissionName("部门状态")
    @RequestMapping("delete")
    @RequiresPermissions("dept:delete")
    @ResponseBody
   public JSONResult delete(Long id){
       JSONResult jsonResult = new JSONResult();
       try {
           deptService.delete(id);
       }catch (Exception e){
           e.printStackTrace();
           jsonResult.setMsg("更改错误.....");
       }
       return jsonResult;

    }


    @RequestMapping("listAll")
    @ResponseBody
    public List<Dept> listAll() {
        return   deptService.listAll();

    }















}